clear all

n = 10000000;

a = 0;
b = 10;
c = (1/2)*(sqrt(21*10)-5*10);


u = rand(n, 1);

x = zeros(n, 1);
for i = 1:n
    U = u(i);
    if U < (c-a)/(b-a)
        X = a + sqrt(U*(b-a)*(c-a));
    else
        X = b - sqrt((1-U)*(b-a)*(b-c));        
    end
    x(i) = X;
end

hist(x, 100);
mean(x)
mode(x)
median(x)
std(x)
var(x)

% simulate a triangular distribution 
%clear all
%n = 10000000;

%a=randint(n,0,10);
%b=randint(n,0,10);

%c=a+b;

%hist(c,max(c)-min(c)+1)
%mean(c)
%mode(c)
%median(c)
%std(c)
%var(c)
